package cn.javass.dp.singleton.example13;

/**
 * Created by wanggen on 19/8/4.
 */
public class Singleton {
    //private static Singleton uniqueInstance;
    //单例模式中用volatile和synchronized来满足双重检查锁机制
    private volatile static Singleton uniqueInstance;

    private Singleton(){
    }

    public static Singleton getInstance(){
        if(uniqueInstance == null){ //#1
            synchronized(Singleton.class){ //#2
                if(uniqueInstance == null){ //#3
                    uniqueInstance = new Singleton(); //#4
                    System.out.println(Thread.currentThread().getName() + ": uniqueInstance is initalized..."); //#5.1
                } else {
                    System.out.println(Thread.currentThread().getName() + ": uniqueInstance is not null now..."); //#5.2
                }
            }
        }
        return uniqueInstance;
    }
}
